<?php
function get_location($num){
	
	return $location;
	
}
function get_index_hot_shop($num=10,$region){
	//热门餐厅
	global $_G;
	$now = gdate('H:i',TIMESTAMP);
	$now = gtime($now);
		
	$worktime1 = array('05:00','05:30','06:00','06:30','07:00','07:30','08:00','08:30','09:00','09:30','10:00','10:30','11:00','11:30');
	$worktime2 = array('12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00','16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30');
		
	foreach($region as $k => $v){
		$return[$k]['value']=$v;
		$return[$k]['shop'] = $_G['db']->fetch_all("SELECT * FROM".table('shops')."WHERE status > 0 AND region='".$k."' ORDER BY recommend DESC LIMIT ".$num);
		if(!empty($return[$k]['shop'])){
			foreach($return[$k]['shop'] as $kv=>$data){
				if($now<gtime($worktime1[$data['opentime']]) || $now > gtime($worktime2[$data['closetime']])){
					$return[$k]['shop'][$kv]['shopclose'] = true;
					$return[$k]['shop'][$kv]['worktime'] = $worktime1[$data['opentime']];
				}
			}
		}	
		if($i++>4)break;
	}
	return 	$return;	
}
function get_index_hot_order($num=10){
	//热门餐厅
	global $_G;
	$query =  $_G['db']->query("SELECT o.txnid,o.gross,s.shopid,s.logo,s.shopname,i.phone FROM".table('orders')." o INNER JOIN".table('shops')." s ON o.touid=s.shopid INNER JOIN".table('orders_shipping')." i ON o.txnid=i.txnid LIMIT ".$num);
	$txnid[0] =0; 
	while($data = $_G['db']->fetch_array($query)){
		$return['orders'][] = $data;
		$txnid[$data['txnid']] = $data['txnid'];
	}
	$query =  $_G['db']->query("SELECT p.txnid,p.pid,p.title FROM".table('orders_product')."p WHERE p.txnid IN(".implode(',', $txnid).")");
	while($data = $_G['db']->fetch_array($query)){
		$return['product'][$data['txnid']][] = $data;
	}
	//vd($return);
	return 	$return;	
}

function get_index_new_dianpin($num=10){
	//热门点评
	global $_G;
	$query =  $_G['db']->query("SELECT o.txnid,s.shopid,s.logo,s.shopname,i.phone FROM".table('orders')." o INNER JOIN".table('shops')." s ON o.touid=s.shopid INNER JOIN".table('orders_shipping')." i ON o.txnid=i.txnid LIMIT ".$num);
	$txnid[0] =0; 
	while($data = $_G['db']->fetch_array($query)){
		$return['orders'][] = $data;
		$txnid[$data['txnid']] = $data['txnid'];
	}
	$query =  $_G['db']->query("SELECT p.txnid,p.pid,p.title FROM".table('orders_product')."p WHERE p.txnid IN(".implode(',', $txnid).")");
	while($data = $_G['db']->fetch_array($query)){
		$return['product'][$data['txnid']][] = $data;
	}
	//vd($return);
	return 	$return;
}
function get_latest_article($num=10){
	global $_G;
	if(ican('view_hidden_category')) {
		$status = '1';
	}else{
		$status = 'c.status=0';
	}
	$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC LIMIT ".$num;
	$a =  $_G['db']->fetch_all($sql);
	return $a;
}
function get_hot_article($num=10){
	global $_G;
	if(ican('view_hidden_category')) {
		$status = '1';
	}else{
		$status = 'c.status=0';
	}
	$sql = "SELECT a.*,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.views DESC LIMIT ".$num;
	$a =  $_G['db']->fetch_all($sql);
	return $a;
}
function get_latest_comment($num=10){
	global $_G;
	$query = $_G['db']->query("SELECT c.cid,c.username,c.content,c.referid,a.title FROM ".table('comment')."c LEFT JOIN".table('article')."a ON a.aid=c.referid WHERE c.type=0 AND c.status=1 ORDER BY c.cid DESC limit ".intval($num));
	while($row = $_G['db']->fetch_array($query)) {
		$comments[$row['cid']] = $row;
	}
	return 	$comments;	
}
function get_index_content($num=14){
	global $_G;
	if(intval($num)<10)$num =14;
	if(ican('view_hidden_category')) {
		$status = '1';
	}else{
		$status = 'c.status=0';
	}
	$sql = "SELECT a.default_image,a.title,a.summary,a.dateline,a.aid,a.comments,c.catename FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC " ;
	$query = $_G['db']->page_query($sql,$num);
	$total = $_G['db']->result("SELECT COUNT(*) FROM ".DB_PREFIX."article a LEFT JOIN ".DB_PREFIX."category c ON a.cid=c.cid WHERE c.type=0 AND $status AND a.ischeck=1 AND a.dateline < ".$_G['timestamp']." ORDER BY a.aid DESC");
	while($data = $_G['db']->fetch_array($query)) {
		$return['content'][] = $data ;
	}
	$return['pager'] = pager($total,$num);
	return $return ;
}
function get_index_link($num=18){
	global $_G;
	return  $_G['db']->fetch_all("SELECT title,url,summary FROM".table('link')."WHERE visible=1 ORDER BY listorder ASC LIMIT ".intval($num));
}
function get_index_ads(){
	return '';
}